﻿<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <base th:href="${#request.getContextPath()}+'/'">
    <head>
        <head>
            <meta charset="utf-8">
            <meta name="renderer" content="webkit|ie-comp|ie-stand">
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
            <meta name="viewport"
                  content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
            <meta http-equiv="Cache-Control" content="no-siteapp"/>
            <link rel="Bookmark" href="/favicon.ico">
            <link rel="Shortcut Icon" href="/favicon.ico"/>
            <!--[if lt IE 9]>
            <script type="text/javascript" src="lib/html5shiv.js"></script>
            <script type="text/javascript" src="lib/respond.min.js"></script>
            <![endif]-->
            <link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css"/>
            <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css"/>
            <link rel="stylesheet" type="text/css" href="lib/Hui-iconfont/1.0.8/iconfont.css"/>
            <link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin"/>
            <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css"/>

            <link rel="stylesheet" href="plugins/bootstrap-ztree/css/bootstrapStyle/bootstrapStyle.css" type="text/css">
            <title>新建网站角色 - 管理员管理 - H-ui.admin v3.1</title>
        </head>
<body>
<article class="page-container">
    <div class="col-md-6">
        <form action="/role/save" method="post" class="form form-horizontal" id="form-admin-role-add">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色名称：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" value="" placeholder="" id="roleName" th:name="roleName">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">备注：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input th:name="roleDesc" type="text" class="input-text">
                </div>
            </div>
            <!-- 隐藏标签 用于接收参数和表单提交业务参数-->
            <input id="moduleCodes" th:name="moduleCodes" type="hidden">
            <input id="treeBeans" th:value="${treeBeans}" type="hidden">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">菜单权限</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <ul id="treeDemo" class="ztree "></ul>
                </div>
            </div>

            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                    <button type="submit" class="btn btn-success radius" id="admin-role-save"><i
                            class="icon-ok"></i> 确定
                    </button>
                </div>
            </div>
        </form>
    </div>


</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/messages_zh.js"></script>

<!--<script type="text/javascript" src="plugins/bootstrap-ztree/js/jquery.min.js"></script>-->
<script type="text/javascript" src="plugins/bootstrap-ztree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="plugins/bootstrap-ztree/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript" src="plugins/bootstrap-ztree/js/jquery.ztree.exedit.js"></script>
<script type="text/javascript">
    $(function () {
        $(".permission-list dt input:checkbox").click(function () {
            $(this).closest("dl").find("dd input:checkbox").prop("checked", $(this).prop("checked"));
        });
        $(".permission-list2 dd input:checkbox").click(function () {
            var l = $(this).parent().parent().find("input:checked").length;
            var l2 = $(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
            if ($(this).prop("checked")) {
                $(this).closest("dl").find("dt input:checkbox").prop("checked", true);
                $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", true);
            }
            else {
                if (l == 0) {
                    $(this).closest("dl").find("dt input:checkbox").prop("checked", false);
                }
                if (l2 == 0) {
                    $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", false);
                }
            }
        });

        $("#form-admin-role-add").validate({
            rules: {
                roleName: {
                    required: true,
                },
            },
            onkeyup: false,
            focusCleanup: true,
            success: "valid",
            submitHandler: function (form) { //只有当表单中，所有需要校验的输入框，都是正确后，才会进入到当前这个方法
                //提交之前，先获取当前选中的所有菜单的ID
                var ids = [];
                var array = $.fn.zTree.getZTreeObj("treeDemo").getCheckedNodes(true);
                for (var i = 0; i < array.length; i++) {
                    ids.push(array[i].id);
                }
                //将树型菜单中选中的所有的菜单 code 赋值给 隐藏输入框 moduleCodes
                $("#moduleCodes").val(ids);

                // $(form).ajaxSubmit();

                $.ajax({
                    type: "post",
                    url: "/role/save",
                    contentType: "application/json; charset=utf-8",
                    cache: false,
                    async: false,
                    data: JSON.stringify($(form).serializeFormJSON()), //转换成json数据
                    success: function (resultData) {
                        alert(resultData.message);
                        if (resultData.success){
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        }
                    }
                });

                //关闭当前弹出框
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);

                // var ids = [];
                // var array = $.fn.zTree.getZTreeObj("treeDemo").getCheckedNodes(true);
                // for (var i = 0; i < array.length; i++) {
                //     ids.push(array[i].id);
                // }
                // $("#moduleCodes").val(ids);
                // //提交数据到后台
                // console.log("form-data============="+JSON.stringify($(form).serializeFormJSON()));
                // $.ajax({
                //     type: "post",
                //     url: "/role/save",
                //     contentType: "application/json; charset=utf-8",
                //     cache: false,
                //     async: false,
                //     data: JSON.stringify($(form).serializeFormJSON()), //转换成json数据
                //     success: function (resultData) {
                //         if (resultData.success){
                //             parent.layer.close(parent.layer.getFrameIndex(window.name));
                //         }
                //         alert(resultData.message);
                //     }
                // });

            }
        });
    });

    (function ($) {
        $.fn.serializeFormJSON = function () {

            var o = {};
            var a = this.serializeArray();
            $.each(a, function () {
                if (o[this.name]) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        };
    })(jQuery);

</script>
<!--/请在上方写此页面业务相关的脚本-->
<script type="text/javascript">
    var setting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };

    $(function () {
        //通过jquery的异步请求，来获取菜单的动态数据
        // $.get("/demo/findMenuList?roleId=92122708", function (data) {
        console.log("======json:==========" + $("#treeBeans").val());
        $.fn.zTree.init($("#treeDemo"), setting, JSON.parse($("#treeBeans").val()));
        // });

        // $("#clickBtn").click(function () {
        //     //获取树型选中的所有节点的id
        //     var array = $.fn.zTree.getZTreeObj("treeDemo").getCheckedNodes(true);
        //     var ids = [];
        //     for (var i = 0; i < array.length; i++) {
        //         ids.push(array[i].id);
        //     }
        //     alert(ids);
        // });

    });
</script>
</body>
</html>